package com.jtpay.ysfproductionprocess.utils;

import android.app.Service;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Vibrator;

import java.lang.reflect.Field;

/**
 * Description:
 * File: AlarmManager.java
 * Author: Administrator
 * Create: 2018/09/05 11:13
 */
public class AlarmSoundManager {
    private static AlarmSoundManager instance;
    private Ringtone ringtone;
    private Vibrator vibrator;
    private AlarmStatusChangedListener changedListener;

    public static AlarmSoundManager getInstance() {
        if (instance == null) {
            instance = new AlarmSoundManager();
        }
        return instance;
    }

    public void play() {
        adjustAlarmVolumeMax();
        getRingtone().play();
        getVibrator().vibrate(new long[]{300, 500}, 0);
        if (changedListener != null) {
            changedListener.alarmStatus(true);
        }
    }

    public void setChangedListener(AlarmStatusChangedListener changedListener) {
        this.changedListener = changedListener;
    }

    public boolean isPlaying() {
        return ringtone != null && ringtone.isPlaying();
    }

    public void stop() {
        if (ringtone != null && ringtone.isPlaying()) {
            ringtone.stop();
            ringtone = null;
        }
        if (vibrator != null) {
            vibrator.cancel();
            vibrator = null;
        }
        if (changedListener != null) {
            changedListener.alarmStatus(false);
        }
    }

    //反射设置闹铃重复播放
    private void setRingtoneRepeat(Ringtone ringtone) {
        Class<Ringtone> clazz = Ringtone.class;
        try {
            Field field = clazz.getDeclaredField("mLocalPlayer");//返回一个 Field 对象，它反映此 Class 对象所表示的类或接口的指定公共成员字段（※这里要进源码查看属性字段）
//            Field audio = clazz.getDeclaredField("mAudio");
            field.setAccessible(true);
            MediaPlayer target = (MediaPlayer) field.get(ringtone);//返回指定对象上此 Field 表示的字段的值
            target.setLooping(true);//设置循环
        } catch (NoSuchFieldException | IllegalAccessException | IllegalArgumentException e) {
            e.printStackTrace();
        }
    }

    private void adjustAlarmVolumeMax() {
        AudioManager audioManager = (AudioManager) AppApplication.getContext().getSystemService(Service.AUDIO_SERVICE);
        if (audioManager == null) {
            return;
        }
        try {
            audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
            int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
            audioManager.setStreamVolume(AudioManager.STREAM_RING, maxVolume, 0);
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.log("adjustAlarmVolumeMax error : " + e.getMessage());
        }
    }

    private Ringtone getRingtone() {
        if (ringtone == null) {
            Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
            ringtone = RingtoneManager.getRingtone(AppApplication.getContext(), uri);
            setRingtoneRepeat(ringtone);
        }
        return ringtone;
    }

    private Vibrator getVibrator() {
        if (vibrator == null) {
            vibrator = (Vibrator) AppApplication.getContext().getSystemService(Context.VIBRATOR_SERVICE);
        }
        return vibrator;
    }

    public interface AlarmStatusChangedListener {
        void alarmStatus(boolean isPlaying);
    }
}
